<?php
class Zend_Lvl_Image{
	public function upload_resizeandcrop($dir_input, $dir_output, $w_size, $h_size){
		try {
			list($width, $height) = getimagesize($dir_input);
			$size_w = $w_size;
			$size_h = $h_size;
			$n_height = $size_h;
			$n_width = round($n_height*$width/$height);
			$image_orig = imagecreatefromjpeg($dir_input);
			$photox = imagesx($image_orig);
			$photoy = imagesy($image_orig);
			$image_fin = imagecreatetruecolor($size_w, $size_h);
			$white = imagecolorallocate($image_fin, 255, 255, 255);
			imagefill($image_fin, 0, 0, $white);
			imagecopyresampled($image_fin, $image_orig, ($size_w - $n_width)/2, 0, 0, 0, $n_width, $n_height, $photox, $photoy);
			imagejpeg($image_fin, $dir_output, 100);
			imagedestroy($image_orig);
			imagedestroy($image_fin);
			
			return true;
		} catch (Exception $e) {
			return false;
		}
		
	}
	public function upload_resize($dir_input, $dir_output, $w_size, $h_size){
		try {
			list($width, $height) = getimagesize($dir_input);
			$size_w = $w_size;
			$size_h = $h_size;
			$image_orig = imagecreatefromjpeg($dir_input);
			$photox = imagesx($image_orig);
			$photoy = imagesy($image_orig);
			$image_fin = imagecreatetruecolor($size_w, $size_h);
			$white = imagecolorallocate($image_fin, 255, 255, 255);
			imagefill($image_fin, 0, 0, $white);
			if($height >= $width){
				$n_height = $size_h;
				$n_width = round($n_height*$width/$height);
				imagecopyresampled($image_fin, $image_orig, ($size_w - $n_width)/2, 0, 0, 0, $n_width, $n_height, $photox, $photoy);
			}else{
				$n_width = $size_w;
				$n_height = round($n_width*$height/$width);
				imagecopyresampled($image_fin, $image_orig, 0, ($size_h - $n_height)/2, 0, 0, $n_width, $n_height, $photox, $photoy);
			}
			imagejpeg($image_fin, $dir_output, 100);
			imagedestroy($image_orig);
			imagedestroy($image_fin);
			
			return true;
		} catch (Exception $e) {
			return false;
		}
		
	}
	
	public function upload_resize_height($dir_input, $dir_output, $h_size){
		try {
			list($width, $height) = getimagesize($dir_input);
			$size_h = $h_size;
			$image_orig = imagecreatefromjpeg($dir_input);
			$photox = imagesx($image_orig);
			$photoy = imagesy($image_orig);
			$n_height = $size_h;
			$n_width = round($n_height*$width/$height);
			if($n_height > $n_width){
				$image_fin = imagecreatetruecolor($n_height, $n_height);
				$white = imagecolorallocate($image_fin, 255, 255, 255);
				imagefill($image_fin, 0, 0, $white);
				imagecopyresampled($image_fin, $image_orig, ($n_height - $n_width)/2, 0, 0, 0, $n_width, $n_height, $photox, $photoy);
			}else{
				$image_fin = imagecreatetruecolor($n_width, $n_height);
				imagecopyresampled($image_fin, $image_orig, 0, 0, 0, 0, $n_width, $n_height, $photox, $photoy);
			}
			imagejpeg($image_fin, $dir_output, 100);
			imagedestroy($image_orig);
			imagedestroy($image_fin);
				
			return true;
		} catch (Exception $e) {
			return false;
		}
	
	}
	
	public function upload_resize_width($dir_input, $dir_output, $w_size){
		try {
			list($width, $height) = getimagesize($dir_input);
			$size_w = $w_size;
			$image_orig = imagecreatefromjpeg($dir_input);
			$photox = imagesx($image_orig);
			$photoy = imagesy($image_orig);
			$n_width = $size_w;
			$n_height = round($n_width*$height/$width);
			$image_fin = imagecreatetruecolor($n_width, $n_height);
			$white = imagecolorallocate($image_fin, 255, 255, 255);
			imagefill($image_fin, 0, 0, $white);
				
			imagecopyresampled($image_fin, $image_orig, 0, 0, 0, 0, $n_width, $n_height, $photox, $photoy);
				
			imagejpeg($image_fin, $dir_output, 100);
			imagedestroy($image_orig);
			imagedestroy($image_fin);
	
			return true;
		} catch (Exception $e) {
			return false;
		}
	
	}
}